<?php

/**
 * uniBoard API Task
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_task extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * Task ID
	 *
	 * @var integer
	 */
	
	public $task_id 				= 0;
	
	/**
	 * Task Name
	 *
	 * @var string
	 */
	
	public $task_name				= '';
	
	/**
	 * Task Description
	 *
	 * @var string
	 */
	
	public $task_description		= '';
	
	/**
	 * Task File
	 *
	 * @var string
	 */
	
	public $task_file				= '';
	
	/**
	 * Task Active
	 *
	 * @var bool
	 */
	
	public $task_active			= true;
	
	/**
	 * Task Days Span
	 *
	 * @var integer
	 */
	
	public $task_days				= 0;
	
	/**
	 * Task Hours Span
	 *
	 * @var integer
	 */
	
	public $task_hours				= 0;
	
	/**
	 * Task Minutes Span
	 *
	 * @var integer
	 */
	
	public $task_minutes			= 0;
	
	/**
	 * Task Next Run
	 *
	 * @var integer
	 */
	
	public $task_next_run			= 0;
							
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store Task ID
		$this -> task_id = (integer) $target_id;
		
		// Autoload If Task ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'tasks
			WHERE task_id = \'' . $this -> task_id . '\'
			LIMIT 1');
			
			// Task found
			if ( $task_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded				= true;
				
				$this -> task_name	 				= $task_result['task_name'];
				$this -> task_description	 		= $task_result['task_description'];
				$this -> task_file	 				= $task_result['task_file'];
				$this -> task_active	 			= $task_result['task_active'];
				$this -> task_days	 				= $task_result['task_days'];
				$this -> task_hours	 				= $task_result['task_hours'];
				$this -> task_minutes	 			= $task_result['task_minutes'];
				$this -> task_next_run	 			= $task_result['task_next_run'];
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_task_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> task_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'tasks');
			
				// Store new Task ID
				$this -> task_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'tasks', 'task_id = \'' . $this -> task_id . '\'');
			}
		
			// Flush cache
			class_cache::flushCache( 'sheduled_tasks');
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Delete API data to DB
	 *
	 */
	
	function deleteData(){
		
		// Delete data
		$this -> appCore -> db -> delete( 'tasks', 'task_id = \'' . $this -> task_id . '\'');
		
		// Flush cache
		class_cache::flushCache( 'sheduled_tasks');
		
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Set task_name
	 *
	 * @param string $new_value
	 */
	
	function setTaskName( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> task_name = $new_value;
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_task_name_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['task_name'] = $this -> appCore -> db -> strEscape( $this -> task_name);
			}
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_task_name_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set task_description
	 *
	 * @param string $new_value
	 */
	
	function setTaskDescription( $new_value){

		// Store in object
		$this -> task_description = $new_value;

		// Add to made changes
		$this -> changes['task_description'] = $this -> appCore -> db -> strEscape( $this -> task_description);
	
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set task_file
	 *
	 * @param string $new_value
	 */
	
	function setTaskFile( $new_value){

		// Store in object
		$this -> task_file = $new_value;

		// Add to made changes
		$this -> changes['task_file'] = $this -> appCore -> db -> strEscape( $this -> task_file);
	
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set task_active
	 *
	 * @param bool $new_value
	 */
	
	function setTaskActive( $new_value){

		// Store in object
		$this -> task_active = $new_value;

		// Add to made changes
		$this -> changes['task_active'] = (bool) $this -> task_active;
	
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set task_days
	 *
	 * @param integer $new_value
	 */
	
	function setTaskDays( $new_value){

		// Store in object
		$this -> task_days = (integer) $new_value;

		// Enforce Mininal Value
		if ( $this -> task_days < 0)
		{
			$this -> task_days = 0;
		}
		
		// Add to made changes
		$this -> changes['task_days'] = $this -> task_days;
	
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set task_hours
	 *
	 * @param integer $new_value
	 */
	
	function setTaskHours( $new_value){

		// Store in object
		$this -> task_hours = (integer) $new_value;

		// Enforce Mininal Value
		if ( $this -> task_hours < 0)
		{
			$this -> task_hours = 0;
		}
		
		// Add to made changes
		$this -> changes['task_hours'] = $this -> task_hours;
	
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set task_minutes
	 *
	 * @param integer $new_value
	 */
	
	function setTaskMinutes( $new_value){

		// Store in object
		$this -> task_minutes = (integer) $new_value;

		// Enforce Mininal Value
		if ( $this -> task_minutes < 0)
		{
			$this -> task_minutes = 0;
		}
		
		// Add to made changes
		$this -> changes['task_minutes'] = $this -> task_minutes;
	
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set task_next_run
	 *
	 * @param integer $new_value
	 */
	
	function setTaskNextRun( $new_value){

		// Store in object
		$this -> task_next_run = (integer) $new_value;

		// Enforce Mininal Value
		if ( $this -> task_next_run < 0)
		{
			$this -> task_next_run = 0;
		}
		
		// Add to made changes
		$this -> changes['task_next_run'] = $this -> task_next_run;
	
	}
								
}